package com.ebelter.myehc.ui.fragments.trends;



import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.products.three2one.ThreeOneManager;

import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.CommonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.views.ChartView3;
import com.ebelter.btcomlib.views.CombinView7;
import com.ebelter.btcomlib.views.CombinView8;
import com.ebelter.myehc.R;

import com.ebelter.myehc.models.http.response.device_resports.Ua_R;
import com.ebelter.myehc.utils.EHCCommonUtils;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8SrcRes;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8Title;

/**
 * 趋势血压
 */

public class Trend_NiaoSuan_Fragment extends BaseFragment {
    public static final String TAG = "Trend_TiWen_Fragment";
    private static final int[] levelColors={0xff14e34e,0xffffcc00};

    CombinView7 ns_cb7;
    CombinView8 ns_cb8;
    ChartView3 trend_ns_chartview3;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_niaosuan;
    }

    public void setReportData(Ua_R reportData) {
        if (reportData==null||reportData.resultCode!=1||reportData.resultData==null){
            return;
        }

        float max = 400;
        if (reportData.resultData.summaryList!=null&&reportData.resultData.summaryList.size()>0){
            max= reportData.resultData.summaryList.get(0).uricacidMax;
           // float min = reportData.resultData.summaryList.get(0).uricacidMin;
        }


        //y轴坐标对应的数据
        List<Float> xyYValue = new ArrayList<>();


        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            xyYValue.add((i *max/5));
        }
        //x轴坐标对应的数据
        List<String> oneXValue = new ArrayList<>();
        List<String> disXValue = new ArrayList<>();
        List<Integer> xPointColor = new ArrayList<>();

        Map<String, Float> xyValue = new HashMap<>();


        float newestNS = 0;

        if (reportData.resultData.chartList!=null){


        //dateType:日期类型 1:年 2:月 3周: 4:天
        //measureType:1:血糖 2:血压 3:血氧 4:体温 5:尿酸 6:总胆固醇 7:人体成份
        for (int i=0;i<reportData.resultData.chartList.size();i++){
            Ua_R.ResultDataBean.ChartListBean data = reportData.resultData.chartList.get(i);


            if (data.uricacidAvg==0){
                continue;
            }
            String timeParam="";
            String disTimeParam="";
            if (reportData.resultData.dateType==1){
                Date date= TimeUtils.parseFormatter91Time(data.timeParam);
                timeParam= CommonUtils.getMonths(NumUtils.string2Int(TimeUtils.formatTime12(date.getTime())));
                disTimeParam=timeParam;
            }else if (reportData.resultData.dateType==2){
                timeParam=String.format(getString_(R.string.Week_int),data.timeParam);
                disTimeParam=timeParam;
            }else if (reportData.resultData.dateType==3){
                Date date2= TimeUtils.parseFormatter3Time(data.timeParam);
                timeParam=TimeUtils.formatTime11(date2.getTime());
                disTimeParam=timeParam;
            }else if (reportData.resultData.dateType==4){
                Date date3= TimeUtils.parseFormatter1Time(data.timeParam);
                timeParam=TimeUtils.formatTime13(date3.getTime());
                disTimeParam=TimeUtils.formatTime4(date3.getTime());

            }

            oneXValue.add(timeParam);
            disXValue.add(disTimeParam);
            xyValue.put(timeParam, data.uricacidAvg);
            newestNS=data.uricacidAvg;


            int result = ThreeOneManager.computerUAHResult(ScaleUser.getUser().getSex(),ScaleUser.getUser().getAge(), (int) data.uricacidAvg);
            xPointColor.add(levelColors[result]);


        }

        }

        if (reportData.resultData.summaryList!=null&&reportData.resultData.summaryList.size()>0){
            setCb8Title(ns_cb8,2, NumUtils.disIntZero((int) reportData.resultData.summaryList.get(0).uricacidMin,N2));
            setCb8Title(ns_cb8,3,NumUtils.disIntZero((int) reportData.resultData.summaryList.get(0).uricacidMax,N2));
            setCb8Title(ns_cb8,4,NumUtils.disIntZero((int) reportData.resultData.summaryList.get(0).uricacidAvg,N2));
            setCb8SrcRes(ns_cb8, EHCCommonUtils.getDisplaySrcRes(reportData.resultData.summaryList.get(0).uricacidAvg,newestNS));
        }

        if (trend_ns_chartview3!=null) {
           // trend_ns_chartview3.yOriStart=reportData.resultData.summaryList.get(0).uricacidMin-5;
            trend_ns_chartview3.displayInt=true;
            trend_ns_chartview3.setxPointColor(xPointColor);
            trend_ns_chartview3.setdisXValue(disXValue);
            trend_ns_chartview3.setValue(xyValue,oneXValue,xyYValue);
            trend_ns_chartview3.src2end();
        }

    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        trend_ns_chartview3=mRootView.findViewById(R.id.trend_ns_chartview3);
        ns_cb8=mRootView.findViewById(R.id.ns_cb8);
        ns_cb7=mRootView.findViewById(R.id.ns_cb7);
        if (AppUtils.en==AppUtils.getLocalStr()){
            ns_cb7.setTitleSizes(13);
            ns_cb8.setTitleSizes(14);
        }
        trend_ns_chartview3.warringTem=1000;
        trend_ns_chartview3.yOriStart=0;


    }

}
